`
https://leetcode.cn/problems/maximum-number-of-removable-characters/
`

/**
 * @param {string} s
 * @param {string} p
 * @param {number[]} removable
 * @return {number}
 */
var maximumRemovals = function (s, p, removable) {
  const n = s.length, pLen = p.length

  const remove = new Array(n).fill(0)
  // removable 前 k 个移除后，p 是否还是 s 的子序列
  const check = (k) => {
    for (let i = 0; i < k; i++) {
      remove[removable[i]] = k
    }
    let j = 0
    for (let i = 0; i < n; i++) {
      if (remove[i] === k) continue
      if (s[i] === p[j]) j++
      if (j === pLen) return true
    }
    return false
  }

  let left = 0, right = removable.length + 1
  while (left + 1 < right) {
    const mid = left + Math.floor((right - left) / 2)
    if (check(mid)) {
      left = mid
    } else {
      right = mid
    }
  }

  return left
};